package com.itcam.phaseii.generic;

import java.util.*;

/**
 * 泛型再HashSet和HashMap的使用情况
 * <p>
 * 演示：<p>
 * 创建3个学生对象<p>
 * 放到HashSet中学生对象使用<p>
 * 放到HashMap中，要求Key是String name，value就是学生对象<p>
 * 使用两种遍历方式<p>
 */

public class GenericExercise {
    public static void main(String[] args) {

        // 使用泛型方式给HashSet放入3个学生对象
        HashSet<Student> students = new HashSet<Student>();
        students.add(new Student("李星云", 25));
        students.add(new Student("姬如雪", 24));
        students.add(new Student("袁天罡", 300));

        // 遍历
        for (Student student : students) {
            System.out.println(student);
        }

        System.out.println("——————————————————————————————————————————");

        // 使用泛型方式给HashMap放入3个学生对象
        HashMap<String, Student> hm = new HashMap<String, Student>();
        // public class HashMap<K, V> {}
        hm.put("张子凡", new Student("张子凡", 25));
        hm.put("倾国", new Student("倾国", 28));
        hm.put("倾城", new Student("倾城", 26));

        // 迭代器 EntrySet
        Set<Map.Entry<String, Student>> entries = hm.entrySet();
        Iterator<Map.Entry<String, Student>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Student> next = iterator.next();
            System.out.println(next.getKey() + "——" + next.getValue());
        }

    }
}

class Student {
    private String name;
    private int age;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
